package com.lh.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.base.Preconditions;

import com.lh.core.BusinessException;
import com.lh.entity.dto.JyQueryDto;
import com.lh.entity.enums.ErrorCodeEnum;
import com.lh.entity.table.DsmsUser;
import com.lh.entity.table.RectificationInfo;
import com.lh.mapper.DsmsUserMapper;
import com.lh.service.DsmsUserService;
import com.lh.utils.Answer;
import com.lh.utils.MD5;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DsmsUserServiceImpl implements DsmsUserService {

    @Resource
    private DsmsUserMapper dsmsUserMapper;

    @Override
    public DsmsUser login(String username, String password) {

        // 校验账号不能为空
        Preconditions.checkArgument(StrUtil.isNotEmpty(username));

        // 校验密码不能为空
        Preconditions.checkArgument(StrUtil.isNotEmpty(password));

        DsmsUser dsmsUser = dsmsUserMapper.selectOne(new LambdaQueryWrapper<DsmsUser>().eq(DsmsUser::getLoginName, username));

        if(dsmsUser == null){
            throw new BusinessException(ErrorCodeEnum.GB10011041);
        }
        if (dsmsUser.getStatus().equals("DISABLE")) {
            throw new BusinessException(ErrorCodeEnum.GB10011052);
        }
        password = MD5.getMd5().getMD5ofStr(password);
        if (!password.equals(dsmsUser.getLoginPwd())) {
            throw new BusinessException(ErrorCodeEnum.GB10011042);
        }
        return dsmsUser;
    }

    @Override
    public Answer<Object> addUser(DsmsUser dsmsUser) {
        try{
            int count = Convert.toInt(dsmsUserMapper.selectCount((Wrappers.<DsmsUser>lambdaQuery().eq(DsmsUser::getLoginName, dsmsUser.getLoginName()))));
            if (count > 0) {
                throw new BusinessException(ErrorCodeEnum.GB10011032);
            }
            if (!checkMobileNo(null, dsmsUser.getMobileNo())) {
                // 存在重复的手机号
                throw new BusinessException(ErrorCodeEnum.GB10011031);
            }
            dsmsUser.setLoginPwd(MD5.getMd5().getMD5ofStr(dsmsUser.getLoginPwd()));
            Integer result = dsmsUserMapper.insert(dsmsUser);
            return Answer.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error(e.getMessage());
        }
    }

    @Override
    public Answer<Object> editUser(DsmsUser dsmsUser) {
        try{
            Integer result = dsmsUserMapper.updateById(dsmsUser);
            return Answer.ok("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error(e.getMessage());
        }
    }

    @Override
    public Answer<Object> deleteUser(DsmsUser dsmsUser) {
        try{
            Long id = dsmsUser.getId();
            if (id == null) {
                return Answer.error("id不能为空");
            }
            int result = dsmsUserMapper.deleteById(id);
            return Answer.ok("删除成功");
        }catch (Exception e){
            return Answer.error(e.getMessage());
        }
    }

    @Override
    public Answer<Object> queryList(JyQueryDto jyQueryDto) {
        try {
            return Answer.ok(dsmsUserMapper.selectList(null));
        } catch (Exception e) {
            return Answer.error(e.getMessage());
        }
    }

    @Override
    public List<DsmsUser> queryUserByName(String userName) {
        LambdaQueryWrapper<DsmsUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DsmsUser::getUserName, userName);
        List<DsmsUser> userList = dsmsUserMapper.selectList(queryWrapper);
        return userList;
    }
    @Override
    public List<DsmsUser> queryUserByDepartment(String department) {
        LambdaQueryWrapper<DsmsUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(DsmsUser::getDepartment, department);
        List<DsmsUser> userList = dsmsUserMapper.selectList(queryWrapper);
        return userList;
    }

    public boolean checkMobileNo(Long userId, String mobileNo) {

        Preconditions.checkArgument(StrUtil.isNotEmpty(mobileNo), "手机号不能为空");

        QueryWrapper<DsmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile_no", mobileNo);

        if (userId != null) {
            queryWrapper.ne("id", userId);
        }
        int result = Convert.toInt(dsmsUserMapper.selectCount(queryWrapper));
        return result < 1;
    }
}
